// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
// 数据库操作对象
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var date = new Date();
  var users = ['张三', '李四', '王五', '赵三', '钱二', '赵云', '关羽', '刘备', '曹操', '孙权', '李白', '杜甫', '张三丰', '张无忌'];
  var websitenames = ['龙坪','农商行2','农商行3','农商行4','农商行5','农商行6','农商行7','农商行8','农商行9','农商行10','农商行11','农商行12','农商行13','农商行14','农商行15','农商行16','农商行17','农商行18','农商行19','农商行20','农商行21','农商行22','农商行23','农商行24','农商行25','农商行26','农商行27','农商行28','农商行29','农商行30','农商行31','农商行32','农商行33','农商行34','农商行35','农商行36','农商行37','农商行38','农商行39','农商行40','农商行41','农商行42','农商行43','农商行44','农商行45','农商行46','农商行47','农商行48','农商行49','农商行50','农商行51','农商行52','农商行53','农商行54','农商行55','农商行56','农商行57','农商行58','农商行59','农商行60','农商行61','农商行62','农商行63','农商行64','农商行65','农商行66','农商行67','农商行68','农商行69','农商行70','农商行71','农商行72','农商行73','农商行74','农商行75','农商行76','农商行77','农商行78','农商行79','农商行80','农商行81','农商行82','农商行83','农商行84','农商行85','农商行86','农商行87','农商行88','农商行89','农商行90','农商行91','农商行92','农商行93','农商行94','农商行95','农商行96','农商行97','农商行98','农商行99','农商行100','农商行101','农商行102','农商行103','农商行104','农商行105','农商行106','农商行107','农商行108','农商行109','农商行110','农商行111','农商行112','农商行113','农商行114','农商行115','农商行116','农商行117','农商行118','农商行119','农商行120','农商行121','农商行122','农商行123','农商行124','农商行125','农商行126','农商行127','农商行128','农商行129','农商行130'];
  var websitecodes =['85090','85190','85191','85192','85193','85194','85195','85196','85197','85198','85199','85200','85201','85202','85203','85204','85205','85206','85207','85208','85209','85210','85211','85212','85213','85214','85215','85216','85217','85218','85219','85220','85221','85222','85223','85224','85225','85226','85227','85228','85229','85230','85231','85232','85233','85234','85235','85236','85237','85238','85239','85240','85241','85242','85243','85244','85245','85246','85247','85248','85249','85250','85251','85252','85253','85254','85255','85256','85257','85258','85259','85260','85261','85262','85263','85264','85265','85266','85267','85268','85269','85270','85271','85272','85273','85274','85275','85276','85277','85278','85279','85280','85281','85282','85283','85284','85285','85286','85287','85288','85289','85290','85291','85292','85293','85294','85295','85296','85297','85298','85299','85300','85301','85302','85303','85304','85305','85306','85307','85308','85309','85310','85311','85312','85313','85314','85315','85316','85317','85318'];
  var name = users[Math.floor(Math.random() * 13)];
  var site = Math.floor(Math.random() * 132);
  var websitename = websitenames[site];
  var websitecode = websitecodes[site];

  console.log("name---->" + name)
  console.log("websitename---->" + websitename)
  console.log("websitecode---->" + websitecode)
  console.log("当前时间是--->" + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds())
  var result = db.collection('es_customer').add({
    data: {
      sfz: '422822199211261036',
      xm: name,
      dh: '13593642238',
      ybcts: 1, //已经保存天数
      rq: date.getFullYear() + '-' + (date.getMonth() + 1) + "-" + date.getDate(),
      xxsj: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(),
      ywlx: '存钱',
      tw: 34,
      wdmc: websitename,
      wdbm: websitecode
    }
  }).then(res => {
    console.log("保存成功");
  }).catch(err => {
    console.log("保存失败");
  })
}